/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.recovery.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.utils.KdniaoTrackQueryAPI;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.goods.entity.GoodsType;
import com.jeesite.modules.goods.service.GoodsTypeService;
import com.jeesite.modules.recovery.entity.ZtRecovery;
import com.jeesite.modules.recovery.service.ZtRecoveryService;

/**
 * 回购Controller
 * 
 * @author ThinkGem
 * @version 2018-04-22
 */
@Controller
@RequestMapping(value = "${adminPath}/ztRecovery")
public class ZtRecoveryController extends BaseController {

	@Autowired
	private ZtRecoveryService ztRecoveryService;
	
	@Autowired
	private GoodsTypeService goodsTypeService;
	
	

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ZtRecovery get(String id, boolean isNewRecord) {
		ZtRecovery ztRecovery =new ZtRecovery();
		ztRecovery.setId(id);
		return ztRecoveryService.get(ztRecovery);
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("ztRecovery:ztRecoveryData:view")
	@RequestMapping(value = { "list", "" })
	public String list(ZtRecovery ztRecovery, Model model) {
		model.addAttribute("ztRecovery", ztRecovery);
		List<GoodsType> typeList=goodsTypeService.findList(new GoodsType());
		model.addAttribute("typeList", typeList);
		
		return "modules/ztRecovery/ztRecoveryList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("ztRecovery:ztRecoveryData:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ZtRecovery> listData(ZtRecovery ztRecovery, HttpServletRequest request,
			HttpServletResponse response,Model model) {
	
		Page<ZtRecovery> page = ztRecoveryService.findPage(new Page<ZtRecovery>(request,
				response), ztRecovery);
		
		
		return page;
	}
	
	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ztRecovery:ztRecoveryData:view")
	@RequestMapping(value = "form")
	public String form(ZtRecovery ztRecovery, Model model,HttpServletRequest request,HttpServletResponse response) {
		
		if (StringUtils.isNotBlank(ztRecovery.getCodestatus())
				&& !"4".equals(ztRecovery.getCodestatus())) {
			String logistics = ztRecovery.getLogistics();
			String logisticsCode = ztRecovery.getLogisticsCode();
			
			if (StringUtils.isNotBlank(logistics)
					&& StringUtils.isNotBlank(logisticsCode)) {
				try {
					KdniaoTrackQueryAPI api = new KdniaoTrackQueryAPI();
					String result = api.getOrderTracesByJson(logisticsCode,
							logistics);

					
					JSONObject job = JSONObject.parseObject(result);
					String state = job.getString("State");
					if ("3".equals(state)) {
						ztRecovery.setCodestatus("4");
						ztRecoveryService.save(ztRecovery);
					}
				} catch (Exception ex) {
					ex.printStackTrace();

				}
			}
		}
		
		model.addAttribute("ztRecovery", ztRecovery);
		List<GoodsType> typeList=goodsTypeService.findList(new GoodsType());
		model.addAttribute("typeList", typeList);
		
		
		
		return "modules/ztRecovery/ztRecoveryForm";
	}
	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ztRecovery:ztRecoveryData:view")
	@RequestMapping(value = "gjForm")
	public String gjForm(ZtRecovery ztRecovery, Model model,HttpServletRequest request,HttpServletResponse response) {
		if (StringUtils.isNotBlank(ztRecovery.getCodestatus())
				&& !"4".equals(ztRecovery.getCodestatus())) {
			String logistics = ztRecovery.getLogistics();
			String logisticsCode = ztRecovery.getLogisticsCode();
			
			if (StringUtils.isNotBlank(logistics)
					&& StringUtils.isNotBlank(logisticsCode)) {
				try {
					KdniaoTrackQueryAPI api = new KdniaoTrackQueryAPI();
					String result = api.getOrderTracesByJson(logisticsCode,
							logistics);

					
					JSONObject job = JSONObject.parseObject(result);
					String state = job.getString("State");
					if ("3".equals(state)) {
						ztRecovery.setCodestatus("4");
						ztRecoveryService.save(ztRecovery);
					}
				} catch (Exception ex) {
					ex.printStackTrace();

				}
			}
		}
		
		
		model.addAttribute("ztRecovery", ztRecovery);
		List<GoodsType> typeList=goodsTypeService.findList(new GoodsType());
		model.addAttribute("typeList", typeList);
		
		
		
		return "modules/ztRecovery/ztRecoveryGjForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("ztRecovery:ztRecoveryData:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ZtRecovery ztRecovery) {
		
		ztRecovery.setCodestatus("2");
		
		ztRecoveryService.save(ztRecovery);
		return renderResult(Global.TRUE, text("估价成功！"));
	}
	

	/**
	 * 删除数据
	 */
	@RequiresPermissions("ztRecovery:ztRecoveryData:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ZtRecovery ztRecovery) {
		ztRecoveryService.delete(ztRecovery);
		return renderResult(Global.TRUE, text("删除回购成功！"));
	}
}